﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FrameworkManapro.BusinessModel;
using FrameworkManapro.WebUI;
using GMAC.ReportingSystem.Business.Entities;
using System.Collections;
using System.Data;

namespace GMAC.ReportingSystem.DataAccess
{
    //-------------------------------------------------
    /// <summary>
    /// Ricardo Recaredo (11/04/2011)
    /// 
    /// DAO Class for Account With Debit Balances Entitie Model
    /// </summary>
    //-------------------------------------------------

    public class AccountDebitBalanceDataAccess : BaseDataAccess<Business.Entities.AccountDebitBalance>
    {
        public override GMAC.ReportingSystem.Business.Entities.AccountDebitBalance Get(object Id)
        {
            ArrayList parameters = new ArrayList() { Id };
            
            DataTable dt = DataAccessComponent.ExecuteSelect("procAccountDebitBalanceByNumberSelect", parameters);

            if (dt != null && dt.Rows.Count > 0)
            {
                return ReadRow(dt.Rows[0]);
            }

            return null;
        }

        //----------------------------------------------------------------------

        public AccountDebitBalanceCollection GetList()
        {
            DataTable dt = DataAccessComponent.ExecuteSelect("procAccountDebitBalanceByNumberSelect", null);

            return ReadTable(dt) as AccountDebitBalanceCollection;
        }

        //----------------------------------------------------------------------

        protected override GMAC.ReportingSystem.Business.Entities.AccountDebitBalance ReadRow(System.Data.DataRow row)
        {
            AccountDebitBalance account = new AccountDebitBalance();
            
            if (row != null)
            {
                try
                {
                    account.AccountNumber    = row["AccountNumber"].ToString();
                    account.AccountTypeID    = new Product { AccountTypeID = int.Parse(row["AccountTypeID"].ToString()) };
                    account.DebitBankNumber  = row["DebitBankNumber"].ToString();
                    account.EmissionDays     = int.Parse(row["EmissionDays"].ToString());
                    account.InsuranceCompany = new InsuranceCompany { Id = int.Parse(row["InsuranceCompanyId"].ToString()), Name = row["InsuranceCompanyName"].ToString() };
                    account.IssueDate        = DateTime.Parse(row["IssueDate"].ToString());
                    account.IssueTime        = (GeneralEnumTypes.EmissionTime)int.Parse(row["IssueTime"].ToString());
                    account.PaymentType      = (GeneralEnumTypes.PaymentType)(bool.Parse(row["PaymentType"].ToString()) ? 1 : 0);
                    account.PendingAmount    = decimal.Parse(row["PendingAmount"].ToString());
                    account.ProjectionDate   = DateTime.Parse(row["ProjectionDate"].ToString());
                    account.StatusLAORetail  = (GeneralEnumTypes.StatusLAORetail)int.Parse(row["StatusLAORetail"].ToString());
                    account.AccountType      = (GeneralEnumTypes.TypeAccountsWithDebitBalances)(bool.Parse(row["Type"].ToString()) ? 1 : 0);
                }
                catch (Exception)
                {
                    throw;
                }
            }
            return account;
        }

        //----------------------------------------------------------------------

        protected GMAC.ReportingSystem.Business.Entities.AccountDebitBalance ReadRowMin(System.Data.DataRow row)
        {
            AccountDebitBalance account = new AccountDebitBalance();

            if (row != null)
            {
                try
                {
                    account.AccountNumber = row["AccountNumber"].ToString();
                    account.AccountTypeID = new Product { AccountTypeID = int.Parse(row["AccountTypeID"].ToString()) };
                    account.DebitBankNumber = row["DebitBankNumber"].ToString();
                    account.InsuranceCompany = new InsuranceCompany { Id = int.Parse(row["InsuranceCompanyId"].ToString()), Name = row["InsuranceCompanyName"].ToString() };
                    account.AccountType = (GeneralEnumTypes.TypeAccountsWithDebitBalances)(bool.Parse(row["Type"].ToString()) ? 1 : 0);
                }
                catch (Exception)
                {
                    throw;
                }
            }
            return account;
        }

        //----------------------------------------------------------------------

        protected override List<AccountDebitBalance> ReadTable(System.Data.DataTable table)
        {
            try
            {
                AccountDebitBalanceCollection accountDebitBalances = new AccountDebitBalanceCollection();
                AccountDebitBalance           accountDebitBalance  = new AccountDebitBalance();

                if (table != null && table.Rows.Count > 0)
                {
                    foreach (DataRow row in table.Rows)
                    {
                        accountDebitBalance = ReadRowMin(row);

                        if (accountDebitBalance != null)
                        {
                            accountDebitBalances.Add(accountDebitBalance);
                        }
                    }
                }
                return accountDebitBalances;
            }
            catch (Exception)
            {
                throw;
            }
        }

        //----------------------------------------------------------------------

        public AccountDebitBalanceCollection Search(string accountNumber, GeneralEnumTypes.TypeAccountsWithDebitBalances? type, int? insuranceCompanyId, DateTime? issueDate, int? emissionDays, GeneralEnumTypes.EmissionTime? emissionTime, PagingDetails paging)
        {
            ArrayList parameters = new ArrayList() { accountNumber, type, insuranceCompanyId, issueDate, emissionDays, emissionTime, paging.PageNumber, paging.PageSize };

            try
            {
                DataSet ds = DataAccessComponent.ExecuteMultipleSelect("procAccountDebitBalancesByFilterSelect", parameters);

                AccountDebitBalanceCollection result = null;
                
                if (ds.Tables.Count == 2)
                {
                    result = ReadTable(ds.Tables[0]) as AccountDebitBalanceCollection;
                    
                    if (ds.Tables[1].Rows.Count > 0)
                    {
                        result.TotalResults = (int)ds.Tables[1].Rows[0][0];
                    }
                }
                return result;
            }
            catch (Exception)
            {
                throw;
            }
        }

        //----------------------------------------------------------------------  
    }
}
